(function($){
    var show = function(aHtml, opt){
    	$("body").css({"overflow":"hidden"});
    	var time = new Date().getTime();
        aHtml = aHtml.replace("#{title}",opt.title).replace("#{text}",'<div id="text_'+time+'"></div>').replace("#{yesBtn}",opt.yesBtn).replace("#{noBtn}",opt.noBtn);
        var al = $(aHtml);
        if(opt.isSubmit){
        	al.find("#text_"+time).append('<textarea name="msg" style="width:100%;padding: 5px;" rows="4">'+opt.text+'</textarea>');
        } else {
        	al.find("#text_"+time).append(opt.text);
        }
        
        al.find(".close,.no").on("click",function(){
            al.remove();
            opt.no();
            $("body").css({"overflow":"auto"});
        });
        al.find(".yes").on("click",function(){
            al.remove();
            opt.yes();
            $("body").css({"overflow":"auto"});
        });
        
        if(opt.customBtn.length>0){
        	al.find(".btn").html("");
        	$.each(opt.customBtn, function(i,v){
        		var btn = $('<button class="yes">'+v+'</button>')
        		btn.click(function(){
        			var msg = al.find("#text_"+time).find("textarea[name='msg']").val();
        			al.remove();
        			$("body").css({"overflow":"auto"});
        			opt.success(v, msg);
        		});
        		al.find(".btn").append(btn);
        	});
        }
        
        
        al.find(".title").on({
            mousedown: function (e) {

                var el2 = $(this);
                var el = el2.parent();
                el.css("transition", "all 0s");
                var os = el.offset();
                var dx = e.pageX - os.left, dy = e.pageY - os.top;
                $(document).on('mousemove.drag', function (e) {
                    el.offset({top: e.pageY - dy, left: e.pageX - dx});
                });
            },
            mouseup: function (e) {
                $(document).off('mousemove.drag');
            }
        });
        if(!opt.hasBtn){
        	al.find(".btn").hide();
        }
        
        $("body").append(al)
        var width = opt.width;
        if(width != null){
        	al.find('.alert').css("width", width+"px");
        }
        var ah = al.find('.alert').height();
        al.find('.alert').css("top", "0px");
        al.find('.alert').css("margin-top", "50px");
        return al;
    }
    var jjalert = function(opt){
        var aHtml = '<div><div class="jj-alert-hide"></div><div class="jj-alert">'+
            '<div class="jj-alert-body">'+
            '<div class="title">'+
            '<label class="label">#{title}</label>'+
            '<span class="close">×</span>'+
            '</div>'+
            '<div class="jj-info">'+
            '<div class="icon"></div>'+
            '<div class="text">#{text}</div>'+
            '</div>'+
            '<div class="btn">'+
            '<button class="yes">#{yesBtn}</button>'+
            '</div>'+
            '</div>'+
            '</div></div>';
        return show(aHtml, opt);
    }
    var jjconfirm=function(opt){

        var aHtml = '<div><div class="jj-alert-hide"></div><div class="jj-alert">'+
            '<div class="jj-alert-body">'+
            '<div class="title">'+
            '<label class="label">#{title}</label>'+
            '<span class="close">×</span>'+
            '</div>'+
            '<div class="jj-info">'+
            '<div class="icon"></div>'+
            '<div class="text">#{text}</div>'+
            '</div>'+
            '<div class="btn">'+
            '<button class="yes">#{yesBtn}</button>'+
            '<button class="no">#{noBtn}</button>'+
            '</div>'+
            '</div>'+
            '</div></div>';
        show(aHtml, opt);
    }
    var options = {
        title:"通知",
        text:"",
        hasBtn:true,
        yesBtn : "确定",
        noBtn:"取消",
        isSubmit:false,
        customBtn:[],//自定义按钮
        success:function(){},
        yes:function(){

        },
        no:function(){

        }
    };

    $.extend({
        jjalert:function(opt) {
            var opt2 = $.extend({}, options, opt||{});
            if(typeof opt == 'string' || opt instanceof jQuery){
                opt2.text = opt;
            }
            return jjalert(opt2);
        },
        jjconfirm:function(opt) {
            var opt2 = $.extend({}, options, opt||{});
            if(typeof opt == 'string'){
                opt2.title='警告';
                opt2.text = opt;
            }
            return jjconfirm(opt2);
        }
    });
})(jQuery);



(function($){
	var options = {
	        html:'<div class="jj-postion"><div class="jj-info"></div></div>'
	    };
	var $retObj = null;
    var jjinfo = function(obj, ret){
    	
        var $tempDiv = $('<div style="overflow: hidden; position: absolute;"></div>'); //用于测量弹出层的隐藏div
    	$(obj).hover(function(evt){
    		if($retObj != null){
            	$retObj.remove();
    		}
    		$retObj = $(options.html);
    		$retObj.find(".jj-info").html(ret);
            
            //添加到隐藏的div中进行测量高度
//            $tempDiv.append($retObj);
            $(document.body).append($retObj);
            var winH = $(window).height();
            var offset4bottom = winH - evt.clientY;
            
            var objL = $(this).offset().left;
            var objT = $(this).offset().top;
            var objW = $(this).width();
            var objH = $(this).height();
            var retObjH = $retObj.height();
            $retObj.css({
        		left : objL+objW,
        		top:objT-(retObjH)+objH
        	});
        	
            
            //$tempDiv.remove();
    		//$(obj).append($retObj)
    	},function(){
    		if($retObj != null){
    			$retObj.hover(function(evt){
        		},function(){
            		$retObj.remove();
        		});
    		}
    		
    		
    	})
    }
    $.fn.extend({
    	jjinfo:function(opt){
        	this.each(function(){
        		jjinfo(this, opt);
            });
        }
    });
})(jQuery);